﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JXT.PrimaryKey.Batman.Log;
using JXT.PrimaryKey.Batman.Container;
using JXT.PrimaryKey.Batman.Validation;
using JXT.PrimaryKey.Batman.Domain.Models;
using JXT.PrimaryKey.Batman.Domain.Repositories;
using JXT.PrimaryKey.Batman.Domain.Validation;
using JXT.PrimaryKey.Batman.Domain.Specifications;

namespace JXT.PrimaryKey.Batman.Domain.Services
{
    /// <summary>
    /// 基础服务基类
    /// </summary>
    /// <typeparam name="TEntity">模型类型</typeparam>
    public abstract class ServiceBase<TEntity> : IService where TEntity : IEntity
    {

        /// <summary>
        /// 关联验证对象
        /// </summary>
        protected virtual IValidator<TEntity> Validator
        {
            get { return ContainerManager.Validation.Resolve<IValidator<TEntity>>(); }
        }

        /// <summary>
        /// 加载指定的验证对象
        /// </summary>
        /// <typeparam name="T">模型类型</typeparam>
        /// <returns>取得的验证对象</returns>
        protected virtual IValidator<T> LoadValidator<T>()
        {
            return ContainerManager.Validation.Resolve<IValidator<T>>();
        }

        /// <summary>
        /// 加载指定的存储对象
        /// </summary>
        /// <typeparam name="TRepository">存储接口类型</typeparam>
        /// <returns>指定的存储对象</returns>
        public TRepository LoadRepository<TRepository>()
        {
            return ContainerManager.Repository.Resolve<TRepository>();
        }

        /// <summary>
        /// 加载指定的服务对象
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <returns></returns>
        public TService LoadService<TService>() where TService : IService
        {
            return ContainerManager.Service.Resolve<TService>();
        }

        /// <summary>
        /// 系统日志对象
        /// </summary>
        public ILogger Logger
        {
            get { return ContainerManager.Default.Resolve<ILogger>(); }
        }

        /// <summary>
        /// 获取存储上下文对象
        /// </summary>
        /// <param name="name">配置名称</param>
        /// <returns></returns>
        public RepositoryContext LoadRepositoryContext(string name = null)
        {
            return ContainerManager.Repository.Resolve<RepositoryContext>(name);
        }

        public virtual RepositoryContext RepositoryContext
        {
            get { return LoadRepositoryContext(); }
        }

        /// <summary>
        /// 条件工厂对象
        /// </summary>
        public Specifications.ISpecificationFactory SpecificationFactory
        {
            get { return ContainerManager.Default.Resolve<ISpecificationFactory>(); }
        }

        /// <summary>
        /// 处理单条数据
        /// </summary>
        /// <param name="entity">数据对象</param>
        protected virtual void FillSingle(TEntity entity)
        {
        }

        /// <summary>
        /// 处理集合数据
        /// </summary>
        /// <param name="source">数据集合</param>
        protected virtual void FillList(IEnumerable<TEntity> source)
        {
            if (source != null)
            {
                foreach (var item in source)
                {
                    FillSingle(item);
                }
            }
        }
    }
}
